package nl.boelen.game.level.tile;

import nl.boelen.game.gfx.Colors;

public abstract class Tile {
    public static final Tile[] tiles = new Tile[256];

    public static final byte VOIDID = 0;
    public static final byte STONEID = 1;
    public static final byte GRASSID = 2;// 3, 4, 5, 6
    public static final byte WATERID = 7;
    public static final byte LAVAID = 8;

    public static final Tile VOID = new BasicSolidTile(VOIDID, 0, 0, Colors.get(000, -1, -1, -1), 0xFF000000);
    public static final Tile STONE = new BasicSolidTile(STONEID, 0, 0, Colors.get(333, -1, -1, -1), 0xFF555555);
    public static final Tile GRASS1 = new BasicTile(GRASSID, 0, 0, Colors.get(131, -1, -1, -1), 0xFF00FF00);
    public static final Tile GRASS2 = new BasicTile(3, 1, 0, Colors.get(-1, 131, 141, -1), 0xFF55FF00);
    public static final Tile GRASS3 = new BasicTile(4, 2, 0, Colors.get(550, 131, 141, -1), 0xFF55FF00);
    public static final Tile GRASS4 = new BasicTile(5, 2, 0, Colors.get(550, 131, 141, -1), 0xFF55FF00, 0x02);
    public static final Tile GRASS5 = new BasicTile(6, 3, 0, Colors.get(550, 131, 141, 450), 0xFF55FF00);
    public static final Tile WATER = new AnimatedTile(WATERID, new int[][] { { 0, 5 }, { 1, 5 }, { 2, 5 }, { 3, 5 },
            { 4, 5 } }, Colors.get(-1, 004, 115, 225), 0xFF0000FF, 1000);
    public static final Tile LAVA = new AnimatedTile(LAVAID, new int[][] { { 0, 6 }, { 1, 6 }, { 2, 6 }, { 3, 6 },
            { 4, 6 } }, Colors.get(500, 511, 531, 541), 0xFFFF0000, 1900);

    private int indexColor;

    protected byte id;
    protected boolean solid;
    protected boolean emitter;

    public Tile(int id, boolean isSolid, boolean isEmitter, int indexColor) {
        this.indexColor = indexColor;
        this.id = (byte) id;
        if (tiles[id] != null) {
            throw new RuntimeException("Duplicate tile id on " + id);
        }
        this.solid = isSolid;
        this.emitter = isEmitter;
        tiles[id] = this;
    }

    public byte getId() {
        return id;
    }

    public boolean isSolid() {
        return solid;
    }

    public boolean isEmitter() {
        return emitter;
    }

    public abstract void tick();

    public int getIndexColor() {
        return indexColor;
    }

    public abstract int getTileId();

    public abstract int getTileMirrorBit();

    public abstract int getTileColor();

    public static boolean isWater(byte id) {
        if (id == WATERID) {
            return true;
        }
        return false;
    }

    public static boolean isLava(byte id) {
        if (id == LAVAID) {
            return true;
        }
        return false;
    }
}
